package com.diyizu.youdianda.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.diyizu.youdianda.dto.YddArticleDto;
import com.diyizu.youdianda.entity.YddArticle;
import com.diyizu.youdianda.entity.YddUserData;
import com.diyizu.youdianda.mapper.YddArticleMapper;
import com.diyizu.youdianda.service.YddArticleService;
import com.diyizu.youdianda.service.YddUserDataService;
import com.diyizu.youdianda.util.JwtUtils;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;

@Service
public class YddArticleServiceImpl extends ServiceImpl<YddArticleMapper, YddArticle> implements YddArticleService {
    @Autowired
    private YddUserDataService yddUserDataService;
    //我的文章
    @Override
    public YddArticleDto myArticle(Integer page, Integer limit) {
        //分页构造器
        Page<YddArticle> articlePage = new Page<>(page, limit);
        //查询结果
        Page<YddArticle> yddArticlePage = this.page(articlePage);

        //装进YddArticleDto类型的集合中
        ArrayList<YddArticle> yddArticles = new ArrayList<>();
        for (YddArticle record : yddArticlePage.getRecords()) {
            yddArticles.add(record);
        }
        //将分页数据逐个封装进dto中
        YddArticleDto yddArticleDto = new YddArticleDto();
        yddArticleDto.setCount(yddArticlePage.getTotal());  //总记录数
        yddArticleDto.setTotalPages(yddArticlePage.getPages()); //总页数
        yddArticleDto.setPageSize(yddArticlePage.getSize());    //每页几条
        yddArticleDto.setCurrentPage(yddArticlePage.getCurrent());  //当前页
        yddArticleDto.setData(yddArticles);
        //返回
        return yddArticleDto;
    }

    //文章列表数据
    @Override
    public HashMap allArticle(int page, int limit, int cateid) {
        //分页构造器
        Page<YddArticle> yddArticlePage = new Page<>(page, limit);
        //根据分类ID拼接条件
        LambdaQueryWrapper<YddArticle> lqw = new LambdaQueryWrapper<>();
        lqw.eq(YddArticle::getCateid, cateid);
        //分页结果
        Page<YddArticle> articlePage = this.page(yddArticlePage, lqw);

        //保证数据格式一致
        YddArticleDto yddArticleDto = new YddArticleDto();
        yddArticleDto.setCount(yddArticlePage.getTotal());  //总记录数
        yddArticleDto.setTotalPages(yddArticlePage.getPages()); //总页数
        yddArticleDto.setPageSize(yddArticlePage.getSize());    //每页几条
        yddArticleDto.setCurrentPage(yddArticlePage.getCurrent());  //当前页
        //将分页数据装进集合中
        ArrayList<YddArticle> yddArticles = new ArrayList<>();
        for (YddArticle record : yddArticlePage.getRecords()) {
            yddArticles.add(record);
        }
        //为dto的data赋值
        yddArticleDto.setData(yddArticles);

        //匹配格式
        HashMap<String, YddArticleDto> hashMap = new HashMap<>();
        hashMap.put("list", yddArticleDto);
        return hashMap;
    }

    @Override
    @Transactional
    public void addData(HttpServletRequest httpServletRequest, Integer type, Integer article_id) {
        String token = httpServletRequest.getHeader("token");
        int useId=0;
        if (JwtUtils.verifyToken(token)) {
            Claims claims = JwtUtils.getClaims(token);
            useId = (int) claims.get("id");
        }
        String currentId = String.valueOf(useId);
        // String currentId = UserID.getCurrentId();
        LambdaQueryWrapper<YddUserData> yddUserDataLambdaQueryWrapper = new LambdaQueryWrapper<>();
        yddUserDataLambdaQueryWrapper.eq(article_id!=null,YddUserData::getArticleId,article_id);
        yddUserDataLambdaQueryWrapper.eq(YddUserData::getUserId,currentId);
        YddUserData one = yddUserDataService.getOne(yddUserDataLambdaQueryWrapper);
        YddUserData yddUserData = new YddUserData();
        yddUserData.setUserId(Integer.valueOf(currentId));
        yddUserData.setArticleId(article_id);
        yddUserData.setType(type);
        yddUserDataService.save(yddUserData);
        if (type==2){
            YddArticle byId = this.getById(article_id);
            String lovenum = byId.getLovenum();
            if (lovenum==null||lovenum.equals("")){
                lovenum="0";
            }
            Integer newLoveNum = Integer.parseInt(lovenum);
            byId.setLovenum(String.valueOf(newLoveNum+1));
            this.updateById(byId);
        }
    }

    @Override
    @Transactional
    public void delData(HttpServletRequest httpServletRequest,Integer type, Integer article_id) {
        String token = httpServletRequest.getHeader("token");
        int useId=0;
        if (JwtUtils.verifyToken(token)) {
            Claims claims = JwtUtils.getClaims(token);
            useId = (int) claims.get("id");
        }
        String currentId = String.valueOf(useId);
       // String currentId = UserID.getCurrentId();
        LambdaQueryWrapper<YddUserData> yddUserDataLambdaQueryWrapper = new LambdaQueryWrapper<>();
        yddUserDataLambdaQueryWrapper.eq(article_id!=null,YddUserData::getArticleId,article_id);
        yddUserDataLambdaQueryWrapper.eq(YddUserData::getUserId,currentId);
        yddUserDataService.remove(yddUserDataLambdaQueryWrapper);
        if (type==2){
            YddArticle byId = this.getById(article_id);
            String lovenum = byId.getLovenum();
            Integer newLoveNum = Integer.parseInt(lovenum);
            byId.setLovenum(String.valueOf(newLoveNum-1));
            this.updateById(byId);
        }
    }
}
